#####################################################################
# Check balance sociotropic sample
#####################################################################

rm(list=ls())

library(ggplot2)
library(stargazer)
library(Cairo)
library(gridExtra)

# Generate log  
#sink("003_balance_socio.txt")

# Read data 
load("002_match_socio.Rdata")

#########################
# Descriptive statistics
#########################

# Before matching

# Gen new dataset for before
d_before_descriptive = data.frame(d$bairroibge,
                                  d$v5a_econpastego_imp,
                                  d$v16e_conversefriends_imp,
                                  d$v21f_trustpoliticians_imp,
                                  d$v22a_prescandsknowyesorno_imp,
                                  d$v22c_presvote,
                                  d$v26a_preswin, 
                                  d$v27_valuescrime_imp,
                                  d$v29_valuesdemocracy_imp,
                                  d$v31_valuesresponsibility_imp,
                                  d$v32_valueshonestorcompetent_imp,
                                  d$v35a_attentionpres_imp,
                                  d$v39a_persuadetoothersyesorno_imp,
                                  d$v40b_thermCUT_imp,
                                  d$v40d_thermbusiness_imp,
                                  d$v41a_thermlula_imp,
                                  d$v41b_thermfhc_imp,
                                  d$v41e_thermserra_imp, 
                                  d$v42a_pidthermpsdb_imp,
                                  d$v42c_pidthermpt_imp,
                                  d$v43a_pidyesorno_imp,
                                  d$v44b_pidparty,
                                  d$v45a_pidvoteyesorno_imp,
                                  d$v45b3_pidvote3psdb,
                                  d$v45b4_pidvote4pt,
                                  d$v46_pidimportant_imp,
                                  d$v50_ideology_imp,
                                  d$v51b_vote98pres,
                                  d$v63a_issuespriv_imp,
                                  d$s1_sex,
                                  d$s4c_crimevictim_imp,
                                  d$s6_education_imp,
                                  d$s7a_jobfixed_imp,
                                  d$s7b_jobformalsector_imp,
                                  d$s7c_jobpublicsector_imp,
                                  d$s7e_jobworry_imp,
                                  d$s7h_jobsearch_imp,
                                  d$s8a_religionaffiliation,
                                  d$s10_age_imp,
                                  d$s11b_raceself,
                                  d$s12_incomefamily_imp,
                                  d$s13a_familynumberadults_imp,
                                  d$s14c_know3fhcparty)

colnames(d_before_descriptive) = c("Neighborhood",
                                   "Egotropic economic evaluation",
                                   "Talk about politics with friends",
                                   "Confidence in politicians",
                                   "Name of one presidential candidate",
                                   "Vote for the incumbent",
                                   "Who do you think will win next election?",
                                   "Opportunities or repression to address crime",
                                   "Democracy or military government",
                                   "Who is responsible for people's economic conditions?",
                                   "Competent or honest candidate",
                                   "Attention to presidential elections",
                                   "Have you persuaded others to vote?",
                                   "Unions feeling thermometer",
                                   "Business sector feeling thermometer",
                                   "Lula feeling thermometer",
                                   "Henrique Cardoso feeling thermometer",
                                   "Jose Serra feeling thermometer",
                                   "PSDB feeling thermometer",
                                   "PT feeling thermometer",
                                   "Do you identify with a party?",
                                   "Which party do you identify with?",
                                   "Do you always vote for the same party?",
                                   "Do you tend to vote for the PSDB?",
                                   "Do you tend to vote for the PT?",
                                   "Importance of party when you vote",
                                   "Ideology",
                                   "Who did you for president in 1998?",
                                   "Opinion about privatization",
                                   "Gender",
                                   "Crime victim",
                                   "Education",
                                   "Stable job",
                                   "Job in the formal sector",
                                   "Job in the public sector",
                                   "Worried about losing job in the future",
                                   "Looking for a job now",
                                   "Religion",
                                   "Age",
                                   "Race",
                                   "Income",
                                   "Number of adults living in your house",
                                   "Do you know the president's party?")

# TABLE 6 APPENDIX
stargazer(d_before_descriptive,
          type = "text",
          colnames = FALSE,
          min.max = FALSE,
          title="Descriptive statistics before matching for sociotropic sample", 
          digits=2, 
          rownames=FALSE, 
          align=TRUE, 
          float = TRUE, 
          float.env = "table", 
          table.placement = "H",
          header = FALSE, 
          no.space = TRUE)
          
# After matching

# Gen new dataset for before
d_after_descriptive = data.frame(d_match$bairroibge,
                                 d_match$v5a_econpastego_imp,
                                 d_match$v16e_conversefriends_imp,
                                 d_match$v21f_trustpoliticians_imp,
                                 d_match$v22a_prescandsknowyesorno_imp,
                                 d_match$v22c_presvote,
                                 d_match$v26a_preswin, 
                                 d_match$v27_valuescrime_imp,
                                 d_match$v29_valuesdemocracy_imp,
                                 d_match$v31_valuesresponsibility_imp,
                                 d_match$v32_valueshonestorcompetent_imp,
                                 d_match$v35a_attentionpres_imp,
                                 d_match$v39a_persuadetoothersyesorno_imp,
                                 d_match$v40b_thermCUT_imp,
                                 d_match$v40d_thermbusiness_imp,
                                 d_match$v41a_thermlula_imp,
                                 d_match$v41b_thermfhc_imp,
                                 d_match$v41e_thermserra_imp, 
                                 d_match$v42a_pidthermpsdb_imp,
                                 d_match$v42c_pidthermpt_imp,
                                 d_match$v43a_pidyesorno_imp,
                                 d_match$v44b_pidparty,
                                 d_match$v45a_pidvoteyesorno_imp,
                                 d_match$v45b3_pidvote3psdb,
                                 d_match$v45b4_pidvote4pt,
                                 d_match$v46_pidimportant_imp,
                                 d_match$v50_ideology_imp,
                                 d_match$v51b_vote98pres,
                                 d_match$v63a_issuespriv_imp,
                                 d_match$s1_sex,
                                 d_match$s4c_crimevictim_imp,
                                 d_match$s6_education_imp,
                                 d_match$s7a_jobfixed_imp,
                                 d_match$s7b_jobformalsector_imp,
                                 d_match$s7c_jobpublicsector_imp,
                                 d_match$s7e_jobworry_imp,
                                 d_match$s7h_jobsearch_imp,
                                 d_match$s8a_religionaffiliation,
                                 d_match$s10_age_imp,
                                 d_match$s11b_raceself,
                                 d_match$s12_incomefamily_imp,
                                 d_match$s13a_familynumberadults_imp,
                                 d_match$s14c_know3fhcparty)

colnames(d_after_descriptive) = c("Neighborhood",
                                   "Egotropic economic evaluation",
                                   "Talk about politics with friends",
                                   "Confidence in politicians",
                                   "Name of one presidential candidate",
                                   "Vote for the incumbent",
                                   "Who do you think will win next election?",
                                   "Opportunities or repression to address crime",
                                   "Democracy or military government",
                                   "Who is responsible for people's economic conditions?",
                                   "Competent or honest candidate",
                                   "Attention to presidential elections",
                                   "Have you persuaded others to vote?",
                                   "Unions feeling thermometer",
                                   "Business sector feeling thermometer",
                                   "Lula feeling thermometer",
                                   "Henrique Cardoso feeling thermometer",
                                   "Jose Serra feeling thermometer",
                                   "PSDB feeling thermometer",
                                   "PT feeling thermometer",
                                   "Do you identify with a party?",
                                   "Which party do you identify with?",
                                   "Do you always vote for the same party?",
                                   "Do you tend to vote for the PSDB?",
                                   "Do you tend to vote for the PT?",
                                   "Importance of party when you vote",
                                   "Ideology",
                                   "Who did you for president in 1998?",
                                   "Opinion about privatization",
                                   "Gender",
                                   "Crime victim",
                                   "Education",
                                   "Stable job",
                                   "Job in the formal sector",
                                   "Job in the public sector",
                                   "Worried about losing job in the future",
                                   "Looking for a job now",
                                   "Religion",
                                   "Age",
                                   "Race",
                                   "Income",
                                   "Number of adults living in your house",
                                   "Do you know the president's party?")

# TABLE 8 APPENDIX
stargazer(d_after_descriptive,
          type = "text",
          colnames = FALSE,
          min.max = FALSE,
          title="Descriptive statistics after matching for sociotropic sample", 
          digits=2, 
          rownames=FALSE, 
          align=TRUE, 
          float = TRUE, 
          float.env = "table", 
          table.placement = "H",
          header = FALSE, 
          no.space = TRUE)

#############################################
# Standardized differences before matching
#############################################

# 1 mom
num0_ego = abs(mean(d$v5a_econpastego_imp[d$t_ind==1])- mean(d$v5a_econpastego_imp[d$t_ind==0]))
den0_ego = sqrt(  ( (sd(d$v5a_econpastego_imp[d$t_ind==1]))^2 + (sd(d$v5a_econpastego_imp[d$t_ind==0]))^2 )/2  )
sd0_ego = num0_ego/den0_ego

# 2 mom
num0_conv = abs(mean(d$v16e_conversefriends_imp[d$t_ind==1])- mean(d$v16e_conversefriends_imp[d$t_ind==0]))
den0_conv = sqrt(  ( (sd(d$v16e_conversefriends_imp[d$t_ind==1]))^2 + (sd(d$v16e_conversefriends_imp[d$t_ind==0]))^2 )/2  )
sd0_conv = num0_conv/den0_conv

# 3 mom
num0_trust = abs(mean(d$v21f_trustpoliticians_imp[d$t_ind==1])- mean(d$v21f_trustpoliticians_imp[d$t_ind==0]))
den0_trust = sqrt(  ( (sd(d$v21f_trustpoliticians_imp[d$t_ind==1]))^2 + (sd(d$v21f_trustpoliticians_imp[d$t_ind==0]))^2 )/2  )
sd0_trust = num0_trust/den0_trust

# 4 mom
num0_preskw = abs(mean(d$v22a_prescandsknowyesorno_imp[d$t_ind==1])- mean(d$v22a_prescandsknowyesorno_imp[d$t_ind==0]))
den0_preskw = sqrt(  ( (sd(d$v22a_prescandsknowyesorno_imp[d$t_ind==1]))^2 + (sd(d$v22a_prescandsknowyesorno_imp[d$t_ind==0]))^2 )/2  )
sd0_preskw = num0_preskw/den0_preskw

# 5 mom
num0_valcrime = abs(mean(d$v27_valuescrime_imp[d$t_ind==1])- mean(d$v27_valuescrime_imp[d$t_ind==0]))
den0_valcrime = sqrt(  ( (sd(d$v27_valuescrime_imp[d$t_ind==1]))^2 + (sd(d$v27_valuescrime_imp[d$t_ind==0]))^2 )/2  )
sd0_valcrime = num0_valcrime/den0_valcrime

# 6 mom
num0_valdem = abs(mean(d$v29_valuesdemocracy_imp[d$t_ind==1])- mean(d$v29_valuesdemocracy_imp[d$t_ind==0]))
den0_valdem = sqrt(  ( (sd(d$v29_valuesdemocracy_imp[d$t_ind==1]))^2 + (sd(d$v29_valuesdemocracy_imp[d$t_ind==0]))^2 )/2  )
sd0_valdem = num0_valdem/den0_valdem

# 7 mom
num0_valres = abs(mean(d$v31_valuesresponsibility_imp[d$t_ind==1])- mean(d$v31_valuesresponsibility_imp[d$t_ind==0]))
den0_valres = sqrt(  ( (sd(d$v31_valuesresponsibility_imp[d$t_ind==1]))^2 + (sd(d$v31_valuesresponsibility_imp[d$t_ind==0]))^2 )/2  )
sd0_valres = num0_valres/den0_valres

# 8 mom
num0_valhon = abs(mean(d$v32_valueshonestorcompetent_imp[d$t_ind==1])- mean(d$v32_valueshonestorcompetent_imp[d$t_ind==0]))
den0_valhon = sqrt(  ( (sd(d$v32_valueshonestorcompetent_imp[d$t_ind==1]))^2 + (sd(d$v32_valueshonestorcompetent_imp[d$t_ind==0]))^2 )/2  )
sd0_valhon = num0_valhon/den0_valhon

# 9 mom
num0_apress = abs(mean(d$v35a_attentionpres_imp[d$t_ind==1])- mean(d$v35a_attentionpres_imp[d$t_ind==0]))
den0_apress = sqrt(  ( (sd(d$v35a_attentionpres_imp[d$t_ind==1]))^2 + (sd(d$v35a_attentionpres_imp[d$t_ind==0]))^2 )/2  )
sd0_apress = num0_apress/den0_apress

# 10 mom
num0_persu = abs(mean(d$v39a_persuadetoothersyesorno_imp[d$t_ind==1])- mean(d$v39a_persuadetoothersyesorno_imp[d$t_ind==0]))
den0_persu = sqrt(  ( (sd(d$v39a_persuadetoothersyesorno_imp[d$t_ind==1]))^2 + (sd(d$v39a_persuadetoothersyesorno_imp[d$t_ind==0]))^2 )/2  )
sd0_persu = num0_persu/den0_persu

# 11 mom
num0_cut = abs(mean(d$v40b_thermCUT_imp[d$t_ind==1])- mean(d$v40b_thermCUT_imp[d$t_ind==0]))
den0_cut = sqrt(  ( (sd(d$v40b_thermCUT_imp[d$t_ind==1]))^2 + (sd(d$v40b_thermCUT_imp[d$t_ind==0]))^2 )/2  )
sd0_cut = num0_cut/den0_cut

# 12 mom
num0_bus = abs(mean(d$v40d_thermbusiness_imp[d$t_ind==1])- mean(d$v40d_thermbusiness_imp[d$t_ind==0]))
den0_bus = sqrt(  ( (sd(d$v40d_thermbusiness_imp[d$t_ind==1]))^2 + (sd(d$v40d_thermbusiness_imp[d$t_ind==0]))^2 )/2  )
sd0_bus = num0_bus/den0_bus

# 13 mom
num0_lula = abs(mean(d$v41a_thermlula_imp[d$t_ind==1])- mean(d$v41a_thermlula_imp[d$t_ind==0]))
den0_lula = sqrt(  ( (sd(d$v41a_thermlula_imp[d$t_ind==1]))^2 + (sd(d$v41a_thermlula_imp[d$t_ind==0]))^2 )/2  )
sd0_lula = num0_lula/den0_lula

# 14 mom
num0_fhc = abs(mean(d$v41b_thermfhc_imp[d$t_ind==1])- mean(d$v41b_thermfhc_imp[d$t_ind==0]))
den0_fhc = sqrt(  ( (sd(d$v41b_thermfhc_imp[d$t_ind==1]))^2 + (sd(d$v41b_thermfhc_imp[d$t_ind==0]))^2 )/2  )
sd0_fhc = num0_fhc/den0_fhc

# 15 mom
num0_serra = abs(mean(d$v41e_thermserra_imp[d$t_ind==1])- mean(d$v41e_thermserra_imp[d$t_ind==0]))
den0_serra = sqrt(  ( (sd(d$v41e_thermserra_imp[d$t_ind==1]))^2 + (sd(d$v41e_thermserra_imp[d$t_ind==0]))^2 )/2  )
sd0_serra = num0_serra/den0_serra

# 16 mom
num0_psdb = abs(mean(d$v42a_pidthermpsdb_imp[d$t_ind==1])- mean(d$v42a_pidthermpsdb_imp[d$t_ind==0]))
den0_psdb = sqrt(  ( (sd(d$v42a_pidthermpsdb_imp[d$t_ind==1]))^2 + (sd(d$v42a_pidthermpsdb_imp[d$t_ind==0]))^2 )/2  )
sd0_psdb = num0_psdb/den0_psdb

# 17 mom
num0_pt = abs(mean(d$v42c_pidthermpt_imp[d$t_ind==1])- mean(d$v42c_pidthermpt_imp[d$t_ind==0]))
den0_pt = sqrt(  ( (sd(d$v42c_pidthermpt_imp[d$t_ind==1]))^2 + (sd(d$v42c_pidthermpt_imp[d$t_ind==0]))^2 )/2  )
sd0_pt = num0_pt/den0_pt

# 18 mom
num0_pi = abs(mean(d$v43a_pidyesorno_imp[d$t_ind==1])- mean(d$v43a_pidyesorno_imp[d$t_ind==0]))
den0_pi = sqrt(  ( (sd(d$v43a_pidyesorno_imp[d$t_ind==1]))^2 + (sd(d$v43a_pidyesorno_imp[d$t_ind==0]))^2 )/2  )
sd0_pi = num0_pi/den0_pi

# 19 mom
num0_piv = abs(mean(d$v45a_pidvoteyesorno_imp[d$t_ind==1])- mean(d$v45a_pidvoteyesorno_imp[d$t_ind==0]))
den0_piv = sqrt(  ( (sd(d$v45a_pidvoteyesorno_imp[d$t_ind==1]))^2 + (sd(d$v45a_pidvoteyesorno_imp[d$t_ind==0]))^2 )/2  )
sd0_piv = num0_piv/den0_piv

# 20 mom
num0_pivpsdb = abs(mean(d$v45b3_pidvote3psdb[d$t_ind==1])- mean(d$v45b3_pidvote3psdb[d$t_ind==0]))
den0_pivpsdb = sqrt(  ( (sd(d$v45b3_pidvote3psdb[d$t_ind==1]))^2 + (sd(d$v45b3_pidvote3psdb[d$t_ind==0]))^2 )/2  )
sd0_pivpsdb = num0_pivpsdb/den0_pivpsdb

# 21 mom
num0_pivpt = abs(mean(d$v45b4_pidvote4pt[d$t_ind==1])- mean(d$v45b4_pidvote4pt[d$t_ind==0]))
den0_pivpt = sqrt(  ( (sd(d$v45b4_pidvote4pt[d$t_ind==1]))^2 + (sd(d$v45b4_pidvote4pt[d$t_ind==0]))^2 )/2  )
sd0_pivpt = num0_pivpt/den0_pivpt

# 22 mom
num0_piimp = abs(mean(d$v46_pidimportant_imp[d$t_ind==1])- mean(d$v46_pidimportant_imp[d$t_ind==0]))
den0_piimp = sqrt(  ( (sd(d$v46_pidimportant_imp[d$t_ind==1]))^2 + (sd(d$v46_pidimportant_imp[d$t_ind==0]))^2 )/2  )
sd0_piimp = num0_piimp/den0_piimp

# 23 mom
num0_ideo = abs(mean(d$v50_ideology_imp[d$t_ind==1])- mean(d$v50_ideology_imp[d$t_ind==0]))
den0_ideo = sqrt(  ( (sd(d$v50_ideology_imp[d$t_ind==1]))^2 + (sd(d$v50_ideology_imp[d$t_ind==0]))^2 )/2  )
sd0_ideo = num0_ideo/den0_ideo

# 24 mom
num0_priv = abs(mean(d$v63a_issuespriv_imp[d$t_ind==1])- mean(d$v63a_issuespriv_imp[d$t_ind==0]))
den0_priv = sqrt(  ( (sd(d$v63a_issuespriv_imp[d$t_ind==1]))^2 + (sd(d$v63a_issuespriv_imp[d$t_ind==0]))^2 )/2  )
sd0_priv = num0_priv/den0_priv

# 25 mom
num0_gen = abs(mean(d$s1_sex[d$t_ind==1])- mean(d$s1_sex[d$t_ind==0]))
den0_gen = sqrt(  ( (sd(d$s1_sex[d$t_ind==1]))^2 + (sd(d$s1_sex[d$t_ind==0]))^2 )/2  )
sd0_gen = num0_gen/den0_gen

# 26 mom
num0_crimvict = abs(mean(d$s4c_crimevictim_imp[d$t_ind==1])- mean(d$s4c_crimevictim_imp[d$t_ind==0]))
den0_crimvict = sqrt(  ( (sd(d$s4c_crimevictim_imp[d$t_ind==1]))^2 + (sd(d$s4c_crimevictim_imp[d$t_ind==0]))^2 )/2  )
sd0_crimvict = num0_crimvict/den0_crimvict

# 27 mom
num0_edu = abs(mean(d$s6_education_imp[d$t_ind==1])- mean(d$s6_education_imp[d$t_ind==0]))
den0_edu = sqrt(  ( (sd(d$s6_education_imp[d$t_ind==1]))^2 + (sd(d$s6_education_imp[d$t_ind==0]))^2 )/2  )
sd0_edu = num0_edu/den0_edu

# 28 mom
num0_jobfix = abs(mean(d$s7a_jobfixed_imp[d$t_ind==1])- mean(d$s7a_jobfixed_imp[d$t_ind==0]))
den0_jobfix = sqrt(  ( (sd(d$s7a_jobfixed_imp[d$t_ind==1]))^2 + (sd(d$s7a_jobfixed_imp[d$t_ind==0]))^2 )/2  )
sd0_jobfix = num0_jobfix/den0_jobfix

# 29 mom
num0_jobfor = abs(mean(d$s7b_jobformalsector_imp[d$t_ind==1])- mean(d$s7b_jobformalsector_imp[d$t_ind==0]))
den0_jobfor = sqrt(  ( (sd(d$s7b_jobformalsector_imp[d$t_ind==1]))^2 + (sd(d$s7b_jobformalsector_imp[d$t_ind==0]))^2 )/2  )
sd0_jobfor = num0_jobfor/den0_jobfor

# 30 mom
num0_jobpub = abs(mean(d$s7c_jobpublicsector_imp[d$t_ind==1])- mean(d$s7c_jobpublicsector_imp[d$t_ind==0]))
den0_jobpub = sqrt(  ( (sd(d$s7c_jobpublicsector_imp[d$t_ind==1]))^2 + (sd(d$s7c_jobpublicsector_imp[d$t_ind==0]))^2 )/2  )
sd0_jobpub = num0_jobpub/den0_jobpub

# 31 mom
num0_jobw = abs(mean(d$s7e_jobworry_imp[d$t_ind==1])- mean(d$s7e_jobworry_imp[d$t_ind==0]))
den0_jobw = sqrt(  ( (sd(d$s7e_jobworry_imp[d$t_ind==1]))^2 + (sd(d$s7e_jobworry_imp[d$t_ind==0]))^2 )/2  )
sd0_jobw = num0_jobw/den0_jobw

# 32 mom
num0_jobse = abs(mean(d$s7h_jobsearch_imp[d$t_ind==1])- mean(d$s7h_jobsearch_imp[d$t_ind==0]))
den0_jobse = sqrt(  ( (sd(d$s7h_jobsearch_imp[d$t_ind==1]))^2 + (sd(d$s7h_jobsearch_imp[d$t_ind==0]))^2 )/2  )
sd0_jobse = num0_jobse/den0_jobse

# 33 mom
num0_age = abs(mean(d$s10_age_imp[d$t_ind==1])- mean(d$s10_age_imp[d$t_ind==0]))
den0_age = sqrt(  ( (sd(d$s10_age_imp[d$t_ind==1]))^2 + (sd(d$s10_age_imp[d$t_ind==0]))^2 )/2  )
sd0_age = num0_age/den0_age

# 34 mom
num0_incfam = abs(mean(d$s12_incomefamily_imp[d$t_ind==1])- mean(d$s12_incomefamily_imp[d$t_ind==0]))
den0_incfam = sqrt(  ( (sd(d$s12_incomefamily_imp[d$t_ind==1]))^2 + (sd(d$s12_incomefamily_imp[d$t_ind==0]))^2 )/2  )
sd0_incfam = num0_incfam/den0_incfam

# 35 mom
num0_famnum = abs(mean(d$s13a_familynumberadults_imp[d$t_ind==1])- mean(d$s13a_familynumberadults_imp[d$t_ind==0]))
den0_famnum = sqrt(  ( (sd(d$s13a_familynumberadults_imp[d$t_ind==1]))^2 + (sd(d$s13a_familynumberadults_imp[d$t_ind==0]))^2 )/2  )
sd0_famnum = num0_famnum/den0_famnum

# 36 mom
num0_fhcparty = abs(mean(d$s14c_know3fhcparty[d$t_ind==1])- mean(d$s14c_know3fhcparty[d$t_ind==0]))
den0_fhcparty = sqrt(  ( (sd(d$s14c_know3fhcparty[d$t_ind==1]))^2 + (sd(d$s14c_know3fhcparty[d$t_ind==0]))^2 )/2  )
sd0_fhcparty = num0_fhcparty/den0_fhcparty

##################################################
# Standardized differences after matching
##################################################

# 1 mom
num1_ego = abs(mean(d$v5a_econpastego_imp[t_id])- mean(d$v5a_econpastego_imp[c_id]))
sd1_ego = num1_ego/den0_ego

# 2 mom
num1_conv = abs(mean(d$v16e_conversefriends_imp[t_id])- mean(d$v16e_conversefriends_imp[c_id]))
sd1_conv = num1_conv/den0_conv

# 3 mom
num1_trust = abs(mean(d$v21f_trustpoliticians_imp[t_id])- mean(d$v21f_trustpoliticians_imp[c_id]))
sd1_trust = num1_trust/den0_trust

# 4 mom
num1_preskw = abs(mean(d$v22a_prescandsknowyesorno_imp[t_id])- mean(d$v22a_prescandsknowyesorno_imp[c_id]))
sd1_preskw = num1_preskw/den0_preskw

# 5 mom
num1_valcrime = abs(mean(d$v27_valuescrime_imp[t_id])- mean(d$v27_valuescrime_imp[c_id]))
sd1_valcrime = num1_valcrime/den0_valcrime

# 6 mom
num1_valdem = abs(mean(d$v29_valuesdemocracy_imp[t_id])- mean(d$v29_valuesdemocracy_imp[c_id]))
sd1_valdem = num1_valdem/den0_valdem

# 7 mom
num1_valres = abs(mean(d$v31_valuesresponsibility_imp[t_id])- mean(d$v31_valuesresponsibility_imp[c_id]))
sd1_valres = num1_valres/den0_valres

# 8 mom
num1_valhon = abs(mean(d$v32_valueshonestorcompetent_imp[t_id])- mean(d$v32_valueshonestorcompetent_imp[c_id]))
sd1_valhon = num1_valhon/den0_valhon

# 9 mom
num1_apress = abs(mean(d$v35a_attentionpres_imp[t_id])- mean(d$v35a_attentionpres_imp[c_id]))
sd1_apress = num1_apress/den0_apress

# 10 mom
num1_persu = abs(mean(d$v39a_persuadetoothersyesorno_imp[t_id])- mean(d$v39a_persuadetoothersyesorno_imp[c_id]))
sd1_persu = num1_persu/den0_persu

# 11 mom
num1_cut = abs(mean(d$v40b_thermCUT_imp[t_id])- mean(d$v40b_thermCUT_imp[c_id]))
sd1_cut = num1_cut/den0_cut

# 12 mom
num1_bus = abs(mean(d$v40d_thermbusiness_imp[t_id])- mean(d$v40d_thermbusiness_imp[c_id]))
sd1_bus = num1_bus/den0_bus

# 13 mom
num1_lula = abs(mean(d$v41a_thermlula_imp[t_id])- mean(d$v41a_thermlula_imp[c_id]))
sd1_lula = num1_lula/den0_lula

# 14 mom
num1_fhc = abs(mean(d$v41b_thermfhc_imp[t_id])- mean(d$v41b_thermfhc_imp[c_id]))
sd1_fhc = num1_fhc/den0_fhc

# 15 mom
num1_serra = abs(mean(d$v41e_thermserra_imp[t_id])- mean(d$v41e_thermserra_imp[c_id]))
sd1_serra = num1_serra/den0_serra

# 16 mom
num1_psdb = abs(mean(d$v42a_pidthermpsdb_imp[t_id])- mean(d$v42a_pidthermpsdb_imp[c_id]))
sd1_psdb = num1_psdb/den0_psdb

# 17 mom
num1_pt = abs(mean(d$v42c_pidthermpt_imp[t_id])- mean(d$v42c_pidthermpt_imp[c_id]))
sd1_pt = num1_pt/den0_pt

# 18 mom
num1_pi = abs(mean(d$v43a_pidyesorno_imp[t_id])- mean(d$v43a_pidyesorno_imp[c_id]))
sd1_pi = num1_pi/den0_pi

# 19 mom
num1_piv = abs(mean(d$v45a_pidvoteyesorno_imp[t_id])- mean(d$v45a_pidvoteyesorno_imp[c_id]))
sd1_piv = num1_piv/den0_piv

# 20 mom
num1_pivpsdb = abs(mean(d$v45b3_pidvote3psdb[t_id])- mean(d$v45b3_pidvote3psdb[c_id]))
sd1_pivpsdb = num1_pivpsdb/den0_pivpsdb

# 21 mom
num1_pivpt = abs(mean(d$v45b4_pidvote4pt[t_id])- mean(d$v45b4_pidvote4pt[c_id]))
sd1_pivpt = num1_pivpt/den0_pivpt

# 22 mom
num1_piimp = abs(mean(d$v46_pidimportant_imp[t_id])- mean(d$v46_pidimportant_imp[c_id]))
sd1_piimp = num1_piimp/den0_piimp

# 23 mom
num1_ideo = abs(mean(d$v50_ideology_imp[t_id])- mean(d$v50_ideology_imp[c_id]))
sd1_ideo = num1_ideo/den0_ideo

# 24 mom
num1_priv = abs(mean(d$v63a_issuespriv_imp[t_id])- mean(d$v63a_issuespriv_imp[c_id]))
sd1_priv = num1_priv/den0_priv

# 25 mom
num1_gen = abs(mean(d$s1_sex[t_id])- mean(d$s1_sex[c_id]))
sd1_gen = num1_gen/den0_gen

# 26 mom
num1_crimvict = abs(mean(d$s4c_crimevictim_imp[t_id])- mean(d$s4c_crimevictim_imp[c_id]))
sd1_crimvict = num1_crimvict/den0_crimvict

# 27 mom
num1_edu = abs(mean(d$s6_education_imp[t_id])- mean(d$s6_education_imp[c_id]))
sd1_edu = num1_edu/den0_edu

# 28 mom
num1_jobfix = abs(mean(d$s7a_jobfixed_imp[t_id])- mean(d$s7a_jobfixed_imp[c_id]))
sd1_jobfix = num1_jobfix/den0_jobfix

# 29 mom
num1_jobfor = abs(mean(d$s7b_jobformalsector_imp[t_id])- mean(d$s7b_jobformalsector_imp[c_id]))
sd1_jobfor = num1_jobfor/den0_jobfor

# 30 mom
num1_jobpub = abs(mean(d$s7c_jobpublicsector_imp[t_id])- mean(d$s7c_jobpublicsector_imp[c_id]))
sd1_jobpub = num1_jobpub/den0_jobpub

# 31 mom
num1_jobw = abs(mean(d$s7e_jobworry_imp[t_id])- mean(d$s7e_jobworry_imp[c_id]))
sd1_jobw = num1_jobw/den0_jobw

# 32 mom
num1_jobse = abs(mean(d$s7h_jobsearch_imp[t_id])- mean(d$s7h_jobsearch_imp[c_id]))
sd1_jobse = num1_jobse/den0_jobse

# 33 mom
num1_age = abs(mean(d$s10_age_imp[t_id])- mean(d$s10_age_imp[c_id]))
sd1_age = num1_age/den0_age

# 34 mom
num1_incfam = abs(mean(d$s12_incomefamily_imp[t_id])- mean(d$s12_incomefamily_imp[c_id]))
sd1_incfam = num1_incfam/den0_incfam

# 35 mom
num1_famnum = abs(mean(d$s13a_familynumberadults_imp[t_id])- mean(d$s13a_familynumberadults_imp[c_id]))
sd1_famnum = num1_famnum/den0_famnum

# 36 mom
num1_fhcparty = abs(mean(d$s14c_know3fhcparty[t_id])- mean(d$s14c_know3fhcparty[c_id]))
sd1_fhcparty = num1_fhcparty/den0_fhcparty

##################
# Generate data
##################

colnames = c(#"Neighborhood",
             "Egotropic economic evaluation",
             "Talk about politics with friends",
             "Confidence in politicians",
             "Name of one presidential candidate",
             #"Vote for the incumbent",
             #"Who do you think will win next election?",
             "Opportunities or repression to address crime",
             "Democracy or military government",
             "Who is responsible for people's economic conditions?",
             "Competent or honest candidates",
             "Attention to presidential elections",
             "Have you persuaded others to vote?",
             "Unions feeling thermometer",
             "Business sector feeling thermometer",
             "Lula feeling thermometer",
             "Henrique Cardoso feeling thermometer",
             "Jose Serra feeling thermometer",
             "PSDB feeling thermometer",
             "PT feeling thermometer",
             "Do you identify with a party?",
             #"Which party do you identify with?",
             "Do you vote always for the same party?",
             "Do you tend to vote for the PSDB?",
             "Do you tend to vote for the PT?",
             "Importance of party when you vote",
             "Ideology",
             #"Who did you for president in 1998?",
             "Opinion about privatization",
             "Gender",
             "Crime victim",
             "Education",
             "Stable job",
             "Job in the formal sector",
             "Job in the public sector",
             "Worried about losing job in the future",
             "Looking for a job now",
             #"Religion",
             "Age",
             #"Race",
             "Income",
             "Number of adults living in your house",
             "Do you know the president's party?")
             
sd0 = c(sd0_ego,
        sd0_conv,
        sd0_trust,
        sd0_preskw,
        sd0_valcrime,
        sd0_valdem,
        sd0_valres,
        sd0_valhon,
        sd0_apress,
        sd0_persu,
        sd0_cut,
        sd0_bus,
        sd0_lula,
        sd0_fhc,
        sd0_serra,
        sd0_psdb,
        sd0_pt,
        sd0_pi,
        sd0_piv,
        sd0_pivpsdb,
        sd0_pivpt,
        sd0_piimp,
        sd0_ideo,
        sd0_priv,
        sd0_gen,
        sd0_crimvict,
        sd0_edu,
        sd0_jobfix,
        sd0_jobfor,
        sd0_jobpub,
        sd0_jobw,
        sd0_jobse,
        sd0_age,
        sd0_incfam,
        sd0_famnum,
        sd0_fhcparty)

sd1 = c(sd1_ego,
        sd1_conv,
        sd1_trust,
        sd1_preskw,
        sd1_valcrime,
        sd1_valdem,
        sd1_valres,
        sd1_valhon,
        sd1_apress,
        sd1_persu,
        sd1_cut,
        sd1_bus,
        sd1_lula,
        sd1_fhc,
        sd1_serra,
        sd1_psdb,
        sd1_pt,
        sd1_pi,
        sd1_piv,
        sd1_pivpsdb,
        sd1_pivpt,
        sd1_piimp,
        sd1_ideo,
        sd1_priv,
        sd1_gen,
        sd1_crimvict,
        sd1_edu,
        sd1_jobfix,
        sd1_jobfor,
        sd1_jobpub,
        sd1_jobw,
        sd1_jobse,
        sd1_age,
        sd1_incfam,
        sd1_famnum,
        sd1_fhcparty)

before = rep("Before",36)
after = rep("After",36)    

meanbalance0 = data.frame(colnames,sd0,before)
meanbalance0$order <- nrow(meanbalance0):1
meanbalance0 <-meanbalance0[order(meanbalance0$order, decreasing=FALSE),]
meanbalance0$colnames <- factor(meanbalance0$colnames, levels = meanbalance0$colnames[order(meanbalance0$order)])

meanbalance1 = data.frame(colnames,sd1,after)
meanbalance1$order <- nrow(meanbalance1):1
meanbalance1 <-meanbalance1[order(meanbalance1$order),]
meanbalance1$colnames <- factor(meanbalance1$colnames, levels = meanbalance1$colnames[order(meanbalance1$order)])

colnames(meanbalance0) = c("covariate","sd","Matching","order")
colnames(meanbalance1) = c("covariate","sd","Matching","order")

meanbalance = rbind(meanbalance0,meanbalance1)
meanbalance = data.frame(meanbalance)

#############################
# Generate plot
#############################

# mean balance
loveplot = ggplot(data = meanbalance, mapping = aes(x = covariate, y = sd, group = Matching, color = Matching)) +
           geom_hline(yintercept = 0.05, color = "black", size = 1, linetype="dashed") +
           coord_flip() +
           theme_bw() + 
           theme(legend.key = element_blank()) +
           labs(y = "Average absolute standardized differences in means") +
           labs(x = "") +
           scale_y_continuous( limits = c(0,0.4), breaks = c(0,0.1,0.2,0.3,0.4) ) +
           scale_color_manual(values=c("gray50","black")) + 
           geom_point(aes(shape=Matching), size=4) +       
           scale_shape_manual(values=c(8,16)) + 
           theme(text = element_text(size=25))

# FIGURE 6 APPENDIX
pdf(file = "figure6_appendix.pdf", width= 15, height = 12,useDingbats=F)
loveplot
dev.off()

# exact balance
d$groups = d$t_ind
d$groups[d$t_ind==0]="Control"
d$groups[d$t_ind==1]="Treated"

d_match$groups = d_match$t_ind
d_match$groups[d_match$t_ind==0]="Control"
d_match$groups[d_match$t_ind==1]="Treated"

# FIGURE 13A APPENDIX
# Exact matching before 
cairo_pdf(file="figure13a_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d, aes(x=v22c_presvote, fill=groups)) + labs(x="Vote for Incumbent", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,1))  + scale_y_continuous(breaks=c(0,100,200,300,400,500,600,700)) + theme(legend.key = element_rect(colour = NA)) +
theme(text = element_text(size=15))
dev.off()

# FIGURE 13B APPENDIX
# Exact matching after
cairo_pdf(file="figure13b_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d_match, aes(x=v22c_presvote, fill=groups)) + labs(x="Vote for Incumbent", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups", values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,1)) + scale_y_continuous(breaks=c(0,100,200,300,400,500,600,700)) + coord_cartesian(ylim=c(0,700)) + theme(legend.key = element_rect(colour = NA)) +
theme(text = element_text(size=15))
dev.off()

# FIGURE 7A APPENDIX
# Near fine balance Neighborhood before
cairo_pdf(file="figure7a_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d, aes(x=bairroibge, fill=groups)) + labs(x="Neighborhood", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,5,10,15,20,25,30,35,40,45)) + theme(legend.key = element_rect(colour = NA)) +
theme(text = element_text(size=15))
dev.off()

# FIGURE 7B APPENDIX
# Near fine balance Neighborhood after
cairo_pdf(file="figure7b_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d_match, aes(x=bairroibge, fill=groups)) + labs(x="Neighborhood", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,5,10,15,20,25,30,35,40,45)) + scale_y_continuous(breaks=c(0,10,20,30,40)) + coord_cartesian(ylim=c(0,40)) + theme(legend.key = element_rect(colour = NA)) + 
theme(text = element_text(size=15))
dev.off()

# FIGURE 8A APPENDIX
# Near fine balance Next president before
cairo_pdf(file="figure8a_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d, aes(x=v26a_preswin, fill=groups)) + labs(x="Who do you think will be the next president", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(1,2,3,4,5,6,7)) + theme(legend.key = element_rect(colour = NA)) +
theme(text = element_text(size=15))
dev.off()

# FIGURE 8B APPENDIX
# Near fine balance Next president before
cairo_pdf(file="figure8b_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d_match, aes(x=v26a_preswin, fill=groups)) + labs(x="Who do you think will be the next president", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(1,2,3,4,5,6,7)) + scale_y_continuous(breaks=c(0,50,100,150,200)) + coord_cartesian(ylim=c(0,200)) + theme(legend.key = element_rect(colour = NA)) +
theme(text = element_text(size=15))
dev.off()

# FIGURE 9A APPENDIX
# Near fine balance Party ID before
cairo_pdf(file="figure9a_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d, aes(x=v44b_pidparty, fill=groups)) + labs(x="Party ID", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,1,2,3,4,5,6,7,8,9,10)) + scale_y_continuous(breaks=c(0,100,200,300,400,500,600)) + theme(legend.key = element_rect(colour = NA)) + theme(legend.key = element_rect(colour = NA)) +
theme(text = element_text(size=15))
dev.off()

# FIGURE 9B APPENDIX
# Near fine balance Party ID after
cairo_pdf(file="figure9b_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d_match, aes(x=v44b_pidparty, fill=groups)) + labs(x="Party ID", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,1,2,3,4,5,6,7,8,9,10)) + scale_y_continuous(breaks=c(0,100,200,300,400,500,600)) + coord_cartesian(ylim=c(0,600)) + theme(legend.key = element_rect(colour = NA)) +
theme(text = element_text(size=15))
dev.off()

# FIGURE 10A APPENDIX
# Near fine balance Religion before
cairo_pdf(file="figure10a_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d, aes(x=s8a_religionaffiliation, fill=groups)) + labs(x="Religion affiliation", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,1,2,3,4,5,6,7,8)) + scale_y_continuous(breaks=c(0,100,200,300,400,500,600)) + coord_cartesian(ylim=c(0,600)) + theme(legend.key = element_rect(colour = NA)) +
theme(text = element_text(size=15))
dev.off()

# FIGURE 10B APPENDIX
# Near fine balance Religion after
cairo_pdf(file="figure10b_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d_match, aes(x=s8a_religionaffiliation, fill=groups)) + labs(x="Religion affiliation", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,1,2,3,4,5,6,7,8)) + scale_y_continuous(breaks=c(0,100,200,300,400,500,600)) + coord_cartesian(ylim=c(0,600)) + theme(legend.key = element_rect(colour = NA)) +
theme(text = element_text(size=15))
dev.off()

# FIGURE 11A APPENDIX
# Near fine balance Race before
cairo_pdf(file="figure11a_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d, aes(x=s11b_raceself, fill=groups)) + labs(x="Race self-identification", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,1,2,3,4,5,6)) + scale_y_continuous(breaks=c(0,100,200,300,400,500)) + theme(legend.key = element_rect(colour = NA)) + theme(legend.key = element_rect(colour = NA)) + 
theme(text = element_text(size=15))
dev.off()

# FIGURE 11B APPENDIX
# Near fine balance Race after
cairo_pdf(file="figure11b_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d_match, aes(x=s11b_raceself, fill=groups)) + labs(x="Race self-identification", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,1,2,3,4,5,6)) + scale_y_continuous(breaks=c(0,100,200,300,400,500)) + coord_cartesian(ylim=c(0,500)) + theme(legend.key = element_rect(colour = NA)) +
theme(text = element_text(size=15))
dev.off()

# FIGURE 12A APPENDIX
# Near fine balance vote98 before
cairo_pdf(file="figure12a_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d, aes(x=v51b_vote98pres, fill=groups)) + labs(x="Preferred candidate 1998 presidential election", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,1,2,3,4,5)) + theme(legend.key = element_rect(colour = NA)) + 
  theme(text = element_text(size=15))
dev.off()

# FIGURE 12B APPENDIX
# Near fine balance vote98 after 
cairo_pdf(file="figure12b_appendix.pdf", 
          width=7, 
          height=7)
ggplot(d_match, aes(x=v51b_vote98pres, fill=groups)) + labs(x="Preferred candidate 1998 presidential election", y="Count") + geom_histogram(binwidth=.5, alpha=.5, position="dodge") + theme_bw() +  scale_fill_manual(name = "Groups",values=c("black", "grey50") ) + scale_x_continuous(breaks=c(0,1,2,3,4,5)) + scale_y_continuous(breaks=c(0,100,200,300)) + coord_cartesian(ylim=c(0,300)) + theme(legend.key = element_rect(colour = NA)) +
  theme(text = element_text(size=15))
dev.off()

#sink()
